package util;

import parser.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class XMFishParsers {
    public static List<Content> parse(List<String> urlList, Properties properties){

        List<Content> contentList = new ArrayList<>();

        //String[] keywords = properties.getProperty("keywords").split(",");
        //for (String keyword : keywords) {

            //Pattern pattern1 = Pattern.compile(keyword);

            for (String url : urlList) {
                String newHtml;
                try {
                    newHtml = Jsoup.connect(url).get().html();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                Document document = Jsoup.parse(newHtml);
                Elements tr3 = document.getElementsByClass("tr3");

                for (Element element : tr3) {

                    Elements subjectTF14 = element.getElementsByClass("subject_t f14");
                    String time = element.getElementsByClass("author").get(0).getElementsByTag("p").text();
                    if (element.selectFirst("tr.tr3.l_note") != null) {
                        continue;
                    }

                    LocalDate date = LocalDate.parse(time);

                    String text = subjectTF14.text();
                    String lastURL = subjectTF14.attr("href");

                    //Matcher matcher1 = pattern1.matcher(text);
                    //if (matcher1.find()) {
                        contentList.add(new Content(text, lastURL, date));
                    //}
                }
            }
        //}

        return contentList;
    }
}
